﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace Housing
{
    public class AreaCheck
    {
        private int _area_id;
        private int _area_noid;
        private string _area_checktime;
        private string _area_signature;
        private string _area_responame;
        private string _apply_id;

        public string Apply_id
        {
            get { return _apply_id; }
            set { _apply_id = value; }
        }

        public int area_id
        {
            get { return _area_id; }
            set { _area_id = value; }
        }
        public int area_noid
        {
            get { return _area_noid; }
            set { _area_noid = value; }
        }
        public string area_checktime
        {
            get { return _area_checktime; }
            set { _area_checktime = value; }
        }
        public string area_signature
        {
            get { return _area_signature; }
            set { _area_signature = value; }
        }
        public string area_responame
        {
            get { return _area_responame; }
            set { _area_responame = value; }
        }

        public AreaCheck()
        {
        }

        public AreaCheck(int areaId)
        {
            GetAreaCheckByAreaId(areaId);
        }

        public void GetAreaCheckByAreaId(int areaId)
        {
            string query = "SELECT * FROM AreaCheck WHERE area_id=" + areaId;

            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.Text, query);

            if (dr.Read())
            {
                _area_id = (int)dr[0];
                _area_noid = (int)dr[1];
                _area_checktime = Utils.GetString(dr[2]);
                _area_signature = Utils.GetString(dr[3]);
                _area_responame = Utils.GetString(dr[4]);
            }

            dr.Close();
        }

        public int AddNewAreaCheck(AreaCheck area)
        {
            string query = "INSERT INTO AreaCheck(area_noid, area_checktime, area_signature, area_responsname,applyid)"
                +"VALUES("+area.area_noid+",'"+area.area_checktime+"','"+area.area_signature+"','"+area.area_responame+"',"+area.Apply_id+")";

            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, query);
        }

        public int UpdateAreaCheckByAreaId(AreaCheck area)
        {
            string query = "UPDATE AreaCheck SET area_noid=" + area.area_noid + ",area_checktimg='" + area.area_checktime + "',"
                + "area_signature='" + area.area_signature + "', area_responsname='" + area.area_responame + "'"
                + "WHERE area_id=" + area.area_id;

            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, query);
        }

        public int DeleteAreaCheckByAreaId(int areaId)
        {
            string query = "DELETE FROM AreaCheck WHERE area_id=" + areaId;

            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, query);
        }

        public bool AddAreaCheck(string applyid, string name, string responsename, string date)
        {
            string cmd = "SELECT COUNT(*) FROM AreaCheck WHERE applyid=" + applyid;
            int succornot = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, CommandType.Text, cmd));
            if (succornot == 0)
            {
                AreaCheck areacheck = new AreaCheck();
                areacheck._area_checktime = date;
                areacheck._area_noid = 0;
                areacheck._area_responame = responsename;
                areacheck._area_signature = name;
                areacheck._apply_id = applyid;
                int succ=areacheck.AddNewAreaCheck(areacheck);
                if (succ>0)
                    return true;
                else
                    return false;
            }
            else
                return false;
        }
    }
}